blog post cover image upload

jamesperet 9 years ago
parent
commit
eb7c64994a

+ 1 - 1
app/controllers/blog_posts_controller.rb

@@ -74,7 +74,7 @@ class BlogPostsController < ApplicationController
74 74
 
75 75
     # Never trust parameters from the scary internet, only allow the white list through.
76 76
     def blog_post_params
77
-      params.require(:blog_post).permit(:title, :slug, :content, :published, :description, :author_id)
77
+      params.require(:blog_post).permit(:title, :slug, :content, :published, :description, :image)
78 78
     end
79 79
     
80 80
     def authenticate_user 

+ 1 - 0
app/models/blog_post.rb

@@ -3,4 +3,5 @@ class BlogPost < ActiveRecord::Base
3 3
   friendly_id :title, use: :slugged
4 4
   validates_presence_of :title, :slug
5 5
   belongs_to :author, :class_name => "User"
6
+  mount_uploader :image, CoverUploader
6 7
 end

+ 71 - 0
app/uploaders/cover_uploader.rb

@@ -0,0 +1,71 @@
1
+# encoding: utf-8
2
+
3
+class CoverUploader < CarrierWave::Uploader::Base
4
+
5
+  # Include RMagick or MiniMagick support:
6
+  # include CarrierWave::RMagick
7
+  include CarrierWave::MiniMagick
8
+  include CarrierWave::MimeTypes
9
+
10
+  # Choose what kind of storage to use for this uploader:
11
+  
12
+  if Rails.env.test? or Rails.env.cucumber?
13
+      storage :file
14
+  end
15
+
16
+  if Rails.env.development?
17
+      storage :file
18
+  end
19
+
20
+  if Rails.env.production?
21
+      # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility if using fog: 
22
+      include Sprockets::Helpers::RailsHelper  
23
+      include Sprockets::Helpers::IsolatedHelper 
24
+      storage :fog
25
+  end
26
+  
27
+  process :set_content_type
28
+
29
+  # Override the directory where uploaded files will be stored.
30
+  # This is a sensible default for uploaders that are meant to be mounted:
31
+  def store_dir
32
+    "uploads/#{mounted_as}/#{model.id}"
33
+  end
34
+  
35
+  def cache_dir
36
+    " ./tmp/uploads/#{mounted_as}/#{model.id}"
37
+  end
38
+
39
+  # Provide a default URL as a default if there hasn't been a file uploaded:
40
+  # def default_url
41
+  #   # For Rails 3.1+ asset pipeline compatibility:
42
+  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
43
+  #
44
+  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
45
+  # end
46
+
47
+  # Process files as they are uploaded:
48
+  # process :scale => [200, 300]
49
+  #
50
+  # def scale(width, height)
51
+  #   # do something
52
+  # end
53
+
54
+  # Create different versions of your uploaded files:
55
+  version :thumb do
56
+     process :resize_to_fit => [300, 200]
57
+  end
58
+
59
+  # Add a white list of extensions which are allowed to be uploaded.
60
+  # For images you might use something like this:
61
+  # def extension_white_list
62
+  #   %w(jpg jpeg gif png)
63
+  # end
64
+
65
+  # Override the filename of the uploaded files:
66
+  # Avoid using model.id or version_name here, see uploader/store.rb for details.
67
+  # def filename
68
+  #   "something.jpg" if original_filename
69
+  # end
70
+
71
+end

+ 1 - 0
app/views/blog_posts/_form.html.erb

@@ -5,6 +5,7 @@
5 5
     <%= f.input :title %>
6 6
     <%= f.input :slug %>
7 7
     <%= f.input :description %>
8
+    <%= f.file_field :image %>
8 9
     <%= f.input :content, class: 'summernote', id: 'post_content' %>
9 10
     <%= f.input :published %>
10 11
   </div>

+ 5 - 1
app/views/blog_posts/index.html.erb

@@ -6,7 +6,11 @@
6 6
     <% @blog_posts.each do |post| %>
7 7
 	   <li class="span3">
8 8
 	     <div class="thumbnail" style="height: 300px;">
9
-	       <img src="http://placehold.it/300x200" alt="">
9
+		  <% if post.image.file != nil %>
10
+		  	<%= image_tag post.image.thumb.to_s %>
11
+		  <% else %>
12
+	       	<img src="http://placehold.it/300x200" alt="">
13
+		  <% end %>
10 14
 	       <h3><%= link_to post.title, post_path(post) %></h3>
11 15
 	       <p><%= post.description %></p>
12 16
 	     </div>

+ 5 - 0
db/migrate/20140923061213_add_thumbnails_to_blog_posts.rb

@@ -0,0 +1,5 @@
1
+class AddThumbnailsToBlogPosts < ActiveRecord::Migration
2
+  def change
3
+    add_column :blog_posts, :image, :string
4
+  end
5
+end

+ 2 - 1
db/schema.rb

@@ -11,7 +11,7 @@
11 11
 #
12 12
 # It's strongly recommended that you check this file into your version control system.
13 13
 
14
-ActiveRecord::Schema.define(version: 20140922074342) do
14
+ActiveRecord::Schema.define(version: 20140923061213) do
15 15
 
16 16
   # These are extensions that must be enabled in order to support this database
17 17
   enable_extension "plpgsql"
@@ -25,6 +25,7 @@ ActiveRecord::Schema.define(version: 20140922074342) do
25 25
     t.datetime "created_at"
26 26
     t.datetime "updated_at"
27 27
     t.string   "description"
28
+    t.string   "image"
28 29
   end
29 30
 
30 31
   add_index "blog_posts", ["author_id"], name: "index_blog_posts_on_author_id", using: :btree

+ 2 - 1
readme.md

@@ -12,7 +12,7 @@ A template for creating rails websites that includes the following:
12 12
 
13 13
 Other features are still under development:
14 14
 
15
-* Email System
15
+* Email System (PaperClip + MailChimp)
16 16
 * Search System
17 17
 * Wiki/Codex
18 18
 - Portfolio
@@ -27,6 +27,7 @@ Other features are still under development:
27 27
 * post form layout
28 28
 * edit account layout
29 29
 * make admin button
30
+
30 31
 * Install instructions
31 32
 * Heroku Deploy button
32 33